<?

switch($act){
	case 'detail':
		ShowDetail(isset($_GET['id'])?$_GET['id']:0);
		$tpl = 'detail';
		break;
	
	case 'search':
		Search();
		$title_page = RESULT_SEARCH;
		$tpl = 'list';
		break;
		
	case 'favourite':
		Favourite();
		$title_page = FAVOURITE_LIST;
		$tpl = 'list';
		break;
		
	default:
		ShowList();
		$tpl="list";
		break;
}

function Favourite()
{
	if(isset($_SESSION['member_id']))
	{
		global $db,$products,$page,$plpage,$set_per_page,$c,$cat,$title_bar,$lang, $idRoot, $idMenuMain, $index_on;
		global $cat1, $cat2, $lg, $prefix_url, $title_page, $FullUrl;
		
		$title_bar = FAVOURITE_LIST;
		
		$cat = $cat2;
		$title_page = $cat2["title_$lg"];
		
		$set_per_page = CST_PRODUCT_PAGING_LIST;
		$sql="select id, name_vn, name_en, size, price, production_vn, production_en, unique_key_vn, unique_key_en, img, cid, seo_f_vn, seo_f_en, view, vote from favourite f, products p where f.fav_pro_id = p.id and fav_mem_id = ".$_SESSION['member_id']." order by p.id desc ";
		
		$c = $db->numRows($db->query($sql));
		$plpage = plpage_seo($sql,$page,$set_per_page);
		$sqlstmt = sqlmod($sql,$page,$set_per_page);	
		$products = $db->getAll($sqlstmt);
	}
}

function ShowList()
{
	global $db,$products,$page,$plpage,$set_per_page,$c,$cat,$title_bar,$lang, $idRoot, $idMenuMain, $index_on;
	global $cat1, $cat2, $cat3, $lg, $prefix_url, $title_page, $FullUrl, $cat;
	
	if (isset($cat3))
		$cat = $cat3;
	else if(isset($cat2)){
		$cat = $cat2;
		$title_bar =  "<a title='".$cat1["name_$lg"]."' href='" . $FullUrl . $prefix_url .$cat1["unique_key_$lg"]. "/'>" .$cat1["name_$lg"]. "</a> > <a title='".$cat2["name_$lg"]."' href='" . $prefix_url .$cat1["unique_key_$lg"]. "/" .$cat2["unique_key_$lg"]. "/'>" .$cat2["name_$lg"]. "</a>";
		}else{
			$cat = $cat1;
			$title_bar =  "<a title='".$cat1["name_$lg"]."' href='" . $FullUrl . $prefix_url .$cat1["unique_key_$lg"]. "/'>" .$cat1["name_$lg"]. "</a>";
		}
	
	$title_page = $cat["title_$lg"];
	
	$set_per_page = CST_PRODUCT_PAGING_LIST;
	
	if($cat['id'] != '')
		$sql="select * from products where active=1 and cid=".$cat['id']." order by num asc, id desc ";
	else
		$sql="select * from products where active=1 order by num asc, id desc ";

	$c = $db->numRows($db->query($sql));
	$plpage = plpage_seo($sql,$page,$set_per_page);
	$sqlstmt = sqlmod($sql,$page,$set_per_page);	
	$products = $db->getAll($sqlstmt);
}

function Search()
{	
	global $db,$products,$page,$plpage,$set_per_page,$c,$cat,$title_bar,$lang, $idRoot, $index_on;
	
	$key = CleanSQLInjection(trim(isset($_GET['key'])?$_GET['key']:''));
	
	if(!empty($key))
	{
		$title_bar =  RESULT_SEARCH;
	
		$set_per_page = CST_PRODUCT_PAGING_SEARCH; 
		
		$sql = "select id, name_vn, name_en, size, price, production_vn, production_en, unique_key_vn, unique_key_en, img, cid, view, vote from products where name_vn like '%".$key."%' or name_en like '%".$key."%' or code like '%".$key."%' or descs_vn like '%".$key."%' or descs_en like '%".$key."%' ";
		
		$c = $db->numRows($db->query($sql));
		$plpage = plpage($sql,$page,$set_per_page);
		$sqlstmt = sqlmod($sql,$page,$set_per_page);	
		$products = $db->getAll($sqlstmt);
	}
}

function ShowDetail($id)
{
	global $db, $product, $products, $img_products, $cat, $title_bar, $anothers, $idRoot, $idMenuMain, $thumbs, $productActive, $products_anews, $product_id;
	global $cat1, $cat2, $cat3, $lg, $prefix_url, $title_page, $keywords, $descriptions, $sizes, $FullUrl, $page, $plpagecomment,$set_per_page, $product_comments, $videos, $certificates;
	
	if(isset($cat3))
		$cat = $cat3;
	else if(isset($cat2)){
		$cat = $cat2;
		$title_bar =  "<a title='".$cat1["name_$lg"]."' href='" . $FullUrl . $prefix_url .$cat1["unique_key_$lg"]. "/'>" .$cat1["name_$lg"]. "</a> > <a title='".$cat2["name_$lg"]."' href='" . $prefix_url .$cat1["unique_key_$lg"]. "/" .$cat2["unique_key_$lg"]. "/'>" .$cat2["name_$lg"]. "</a>";
		}else{
			$cat = $cat1;
			$title_bar =  "<a title='".$cat1["name_$lg"]."' href='" . $FullUrl . $prefix_url .$cat1["unique_key_$lg"]. "/'>" .$cat1["name_$lg"]. "</a>";
		}
	
	$product_key = CleanSQLInjection(trim(isset($_GET['unique_key'])?$_GET['unique_key']:''));
	$sql = "select * from products where unique_key_$lg='$product_key' and cid=" . $cat['id'];

	$productActive = $product = $db->getRow($sql);
	$product_id = $product['id'];
	
	CountProductView($product_id, $db);
	//seo
	$title_page = $product["title_$lg"];
	$keywords = $product["keyword_$lg"];
	$descriptions = $product["des_$lg"];
	
	$sql="select * from products where active=1 and cid=".$product['cid']." and id<>$product_id order by num asc, view desc, id desc limit 0,10";
	$products_anews = $db->getAll($sql);
		
	$set_per_page = CST_PRODUCT_PAGING_COMMENT;
	$sql = "select * from comments where cmt_pro_id = ".$product['id']." and active = 1 order by cmt_id desc";
	
	$plpagecomment = plpage_seo($sql,$page,$set_per_page);
	$sqlstmt = sqlmod($sql,$page,$set_per_page);
	$product_comments = $db->getAll($sqlstmt);
	
	//$sql = "select * from video where cid=".$product['id']." and active=1 order by num asc, id desc limit 6";
	//$videos = $db->getAll($sql);
	
	//$sql = "select * from certificates where cid=".$product['id']." and active=1 order by num asc, id desc limit 6";
	//$certificates = $db->getAll($sql);	
	
	$sql = "select * from img where product_id=".$product['id']." and active=1 order by num asc, id desc limit 6";
	$img_products = $db->getAll($sql);	
	
}

function CountProductView($ProductId, $db){
	$UniqueSession = "ProductsAreViewed";
	$ProductIdString = "[" . $ProductId . "]";
	
	if(!isset($_SESSION[$UniqueSession]))
	{
		$_SESSION[$UniqueSession] = $ProductIdString;
		
		$sql = "update products set view = view + 1 where id = $ProductId";	
		$db->query($sql);
	}
	else
	{
		$mystring = $_SESSION[$UniqueSession];
		$findme = $ProductIdString;
		$pos = strpos($mystring, $findme);
		
		if($pos === false)
		{
			$_SESSION[$UniqueSession] .= $ProductIdString;
			
			$sql = "update products set view = view + 1 where id = $ProductId";	
			$db->query($sql);
		}
	}
}

?>